@file:JvmName("LongExtensions")

package com.example.mytestdemo.extensions

import android.annotation.SuppressLint
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created on 2020/8/18,@author Zen.
 * E-mail:jyzen@foxmail.com
 * This is ground control to Major Tom.
 * Don't panic.
 */

data class WPTDuration(var hours: Int, var minus: Int, var secs: Int) {
    fun formatHour() : String {
        return if (hours < 10) "0$hours" else hours.toString()
    }

    fun formatMinus() : String {
        return if (minus < 10) "0$minus" else minus.toString()
    }

    fun formatSec() : String {
        return if (secs < 10) "0$secs" else secs.toString()
    }
}

data class WPTMillsDuration(var minus: Int, val secs: Int, val mills: Int)

data class WPTDayDuration(var day: Int, var hour: Int, var minus: Int, val secs: Int)

data class WPTHourMillsDuration(var hour: Int, var minus: Int, val secs: Int, val mills: Int)

data class WPTHourMillsTotalDuration(
    /** 倒计时 */
    var time: Long, var hour: Int, var minus: Int, val secs: Int, val mills: Int)

/**
 * 输入单位:秒s
 * 计算时间片段 xx小时xx分xx秒
 */
fun Long.calcDuration(): WPTDuration = WPTDuration((this / 3600).toInt(), (this % 3600 / 60).toInt(), (this % 3600 % 60).toInt())

/**
 * 输入单位:毫秒ms
 * xx分xx秒xx(毫秒值0-9)
 */
fun Long.calcMillsDuration(): WPTMillsDuration = WPTMillsDuration((this / 60_000).toInt(), (this % 60_000 / 1000).toInt(), (this % 60_000 % 1000 / 100).toInt())

/**
 * 输入单位:毫秒ms
 * xx分xx秒xx(毫秒值0-99)
 */
fun Long.calcMs10Duration(): WPTMillsDuration = WPTMillsDuration((this / 60_000).toInt(), (this % 60_000 / 1000).toInt(), (this % 60_000 % 1000 / 10).toInt())

/**
 * 输入单位:毫秒ms
 * xx时xx分xx秒xx(毫秒值0-9)
 */
fun Long.calcHourMs10Duration(): WPTHourMillsDuration = WPTHourMillsDuration((this / 3600_000).toInt(), (this % 3600_000 / 60_000).toInt(), (this % 60_000 / 1000).toInt(), (this % 60_000 % 1000 / 100).toInt())

/**
 * 输入单位:毫秒ms
 * xx时xx分xx秒xx(毫秒值0-9)
 */
fun Long.calcHourMs10TotalDuration(): WPTHourMillsTotalDuration = WPTHourMillsTotalDuration(this, (this / 3600_000).toInt(), (this % 3600_000 / 60_000).toInt(), (this % 60_000 / 1000).toInt(), (this % 60_000 % 1000 / 100).toInt())

/**
 * 输入单位:秒 s
 * xx天xx时xx分xx秒xx
 */
fun Long.calcDayDuration(): WPTDayDuration = WPTDayDuration( (this / (24 * 3600)).toInt(), (this % (24 * 3600) / 3600).toInt(), (this % 3600 / 60).toInt(), (this % 60).toInt())


/**
 * 输入单位:秒s
 * 将时间戳转换成pattern对应时间串.
 * 防止手机设置时区问题
 *
 * ps:服务器给的时间戳全是秒值,所以*1000!!!
 */
@SuppressLint("SimpleDateFormat")
fun Long.format2TimeStr(pattern: String = "yyyy-MM-dd HH:mm:ss") = SimpleDateFormat(pattern).apply { timeZone = TimeZone.getTimeZone("Asia/Shanghai") }.format(Date(this * 1000))

fun Long.format2TimeStrHHmm(): String {
    return format2TimeStr("HH:mm")
}
fun Long.format2TimeStrMMss(): String {
    return format2TimeStr("mm:ss")
}

fun Long.format2TimeStrHHmmss(): String {
    return format2TimeStr("HH:mm:ss")
}

fun Long.format2TimeStrYMD(): String {
    return format2TimeStr("yyyy年MM月dd日")
}

fun Long.format2TimeStrYMDHHmm(): String {
    return format2TimeStr("yyyy-MM-dd HH:mm")
}

/**
 * @param currentTime 当前时间 可以传入服务器时间，不传默认使用系统时间  单位 s
 * @return 返回值为 当前时间与要比较时间相差的天数  大于0 在当前时间之后  小于0 在当前时间之前
 */
fun Long.compareWithCurrentTime(currentTime: Long? = null): Int {
    val curTime = currentTime ?: System.currentTimeMillis() / 1000
    return ((this.getYearMonthDayTimeMillis() - curTime.getYearMonthDayTimeMillis()) / (60 * 60 * 24 * 1000)).toInt()
}

/**
 * 获取当前时区给定时间零点的时间戳
 * 原时间戳 s
 * @return ms
 */
fun Long.getYearMonthDayTimeMillis(): Long {
    val curTime = this
    val calendar = Calendar.getInstance().apply {
        time = Date(curTime * 1000)
        set(Calendar.HOUR_OF_DAY, 0)
        set(Calendar.MINUTE, 0)
        set(Calendar.SECOND, 0)
    }
    return calendar.time.time
}

/**
 * 获取安全Long值
 */
fun Long?.safe(default: Long = 0): Long {
    return this ?: default
}